package SqList;
import LinkList.Item;
public class List {
    private Item[] a = new Item[100];
    private int size = 0;
    private int current;

    public List() {
        int i = 0;
        size = 0;
        for (i = 0; i < 100; i++)
            a[i] = null;
    }

    public int getSize() {
        return this.size;
    }

    public void listAll() {
        for (int i = 0; i < size; i++) {
            System.out.print(" " + a[i].getCoef() + "y^" + a[i].getExp()+"");
            System.out.print(i < size-1  ? "+":"");
        }
    }

    public void addAt(int index, Item data) {
        if (index < 0 || index > size) {
            System.out.println("the index error");
        } else {
            for (int i = size - 1; i >= index; i--) {
                a[i + 1] = a[i];
            }
            a[index] = data;
            this.size++;
        }
    }

    public Item getData(int i) {
        if (i >= 0 && i < size)
            return a[i];
        else
            return null;

    }

    public List mergeChain(List list1, List list2) {
        int index = 0;
        int i = 0;
        int j = 0;
        List list3 = new List();
        while (i < list1.getSize() && j < list2.getSize()) {
            if (list1.getData(i).getExp() > list2.getData(j).getExp()) {
                list3.addAt(index, list2.getData(j));
                j++;
                index++;
            } else if (list1.getData(i).getExp() < list2.getData(j).getExp()) {
                list3.addAt(index, list1.getData(i));
                i++;
                index++;
            } else {

                int coef = list1.getData(i).getCoef()
                        + list2.getData(j).getCoef();
                int exp = list1.a[i].getExp();
                Item item = new Item();
                item.setCoef(coef);
                item.setExp(exp);
                if (coef != 0) {
                    list3.addAt(index, item);
                    i++;
                    j++;
                    index++;
                }

            }
        }
        while (i < list1.getSize()) {

            list3.addAt(index++, list1.getData(i++));

        }
        while (j < list2.getSize()) {
            list3.addAt(index++, list2.getData(j++));
        }

        return list3;
    }


}
